//
//  AboutUSVC.m
//  DocumentWatermarking
//
//  Created by apple on 2018/9/25.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "AboutUSVC.h"
#import "HelpCenterViewController.h"
#import "FeatureSuggestionsViewController.h"
#import "AppDelegate.h"
#import "NetWorkRequest.h"
#import "VersionUpDataView.h"
#import <KVNProgress.h>

@interface AboutUSVC ()
@property (weak, nonatomic) IBOutlet UIImageView *headIcon;
@property (weak, nonatomic) IBOutlet UIButton *helpButton;
@property (weak, nonatomic) IBOutlet UIButton *suggestButton;
@property (weak, nonatomic) IBOutlet UIButton *changeButton;
@property (weak, nonatomic) IBOutlet UIButton *updateButton;
@property (weak, nonatomic) IBOutlet UILabel *redLabel;

@end

@implementation AboutUSVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestNewVersion];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.backCall = ^{
//        [self.navigationController popToRootViewControllerAnimated:YES];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isFirst"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        UIStoryboard *UpgradeHardware = [UIStoryboard storyboardWithName:@"Main" bundle:nil];//UpgradeHardware storyboard的名称
        UIViewController *firstVC = [UpgradeHardware instantiateViewControllerWithIdentifier:@"MainViewController"];
        
        AppDelegate*dele = (AppDelegate*)[UIApplication sharedApplication].delegate;
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:firstVC];
        
        dele.window.rootViewController = nav;
        [dele.window makeKeyAndVisible];
        
    };
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _headIcon.layer.cornerRadius = 8;
    _headIcon.layer.masksToBounds = YES;
    
    _helpButton.layer.cornerRadius = 22;
    _helpButton.layer.masksToBounds = YES;
    _helpButton.layer.borderColor = [UIColor colorWithHexString:@"cccccc"].CGColor;
    _helpButton.layer.borderWidth = 1;
    
    _suggestButton.layer.cornerRadius = 22;
    _suggestButton.layer.masksToBounds = YES;
    _suggestButton.layer.borderColor = [UIColor colorWithHexString:@"cccccc"].CGColor;
    _suggestButton.layer.borderWidth = 1;
    
    _updateButton.layer.cornerRadius = 22;
    _updateButton.layer.masksToBounds = YES;
    _updateButton.layer.borderColor = [UIColor colorWithHexString:@"cccccc"].CGColor;
    _updateButton.layer.borderWidth = 1;
    
    _changeButton.layer.cornerRadius = 22;
    _changeButton.layer.masksToBounds = YES;
    _changeButton.hidden = YES;
    
    _redLabel.layer.cornerRadius = 4;
    _redLabel.layer.masksToBounds = YES;
    
}


- (IBAction)helpAction:(UIButton *)sender {
    HelpCenterViewController * vc = [[HelpCenterViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)suggestAction:(UIButton *)sender {
    FeatureSuggestionsViewController * vc = [[FeatureSuggestionsViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

/*
 *  请求新的版本信息
 */
-(void)requestNewVersion{
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    //设备标识
    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    
    [[NetWorkRequest sharedNetWork]GET:kGetVersion delegate:nil parameters:@{@"timestamp":safeString([Tools getNowTimeTimestamp]),@"platform":@"ios",@"channel":@"apple",@"version":safeString(app_Version),@"device":safeString(idfv)}.mutableCopy success:^(id responseObject) {
        
        NSLog(@"新的版本==%@",responseObject);
        
        if (responseObject&&responseObject[@"data"]) {
            
            NSDictionary *dic = responseObject[@"data"];
            
            NSString * note = dic[@"note"];
            NSLog(@"tishiyu======%@",note);
            NSString *code = dic[@"code"];
            //获取手机程序的版本号
            NSString *ver = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"];
            
            if ([ver compare:code] == NSOrderedAscending) {
                VersionUpDataView *view = [[VersionUpDataView alloc]initWithShowFrame:self.view.bounds andAlpha:0.3];
                //                NSString * str = @"升级提示";
                CGSize size = [note boundingRectWithSize:CGSizeMake(view.bounds.size.width-130, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSMutableDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil].size;
                
                CGFloat height = size.height+ 15;
                view.updatePromptText.frame = CGRectMake(15, (kScreenWidth - 100)/275 * 385/2 + 38, view.bounds.size.width-130, height);
                
                view.updatePromptText.text = note ;
                
                WeakSelf;
                view.updateAction = ^{
                    
                    [weakSelf updateAction];
                    
                };
                [view show];
                _redLabel.hidden = NO;
            }else{
                _redLabel.hidden = YES;
            }
            
        }
        
        
    } failure:^(NSError *error) {
        
        NSLog(@"请求错误");
        
    }];
    
}

//升级，跳转到App Store
-(void)updateAction{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id1328253837"]];
    if (IOS10_OR_LATER) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
    else{
        [[UIApplication sharedApplication] openURL:url];
    }
}

//检查更新
- (IBAction)updateAction:(UIButton *)sender {
    
    if (_redLabel.hidden) {
        [KVNProgress showErrorWithStatus:@"没有更新了"];
    }else{
        [self updateAction];
    }
    
    
}


@end
